﻿namespace WMS6.Web.Configuration
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoDetailServiceRef;
    using WMS6.Web.CommonMethodServiceRef;

    public class AllocateRuleDetail : BasePage
    {
        protected Button btnCancel;
        protected Button btnSave;
        protected Button btnSaveContinue;
        protected CheckBox chbIsActive;
        protected DropDownList ddlAllocateStrategy;
        protected DropDownList ddlUomID;
        protected HtmlForm F2203;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnType;
        protected Label lblAllocateFilter;
        protected Label lblAllocateRuleDetail;
        protected Label lblAllocateRuleID;
        protected Label lblAllocateStrategy;
        protected Label lblDescr;
        protected Label lblLineID;
        protected Label lblMsg;
        protected Label lblUomID;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtAllocateFilter;
        protected TextBox txtAllocateRuleID;
        protected TextBox txtDesrc;
        protected TextBox txtLineID;

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            base.Response.Redirect(string.Format("AllocateRule.aspx?from={0}&detail={1}&ID={2}", base.UrlEncode(this.hdnFrom.Value), "1", base.UrlEncode(this.txtAllocateRuleID.Text.Trim())));
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            bool success = false;
            try
            {
                AllocateRuleDetailInfo allocateRuleDetail = new AllocateRuleDetailInfo
                                                                {
                                                                    AllocateRuleID = this.txtAllocateRuleID.Text.Trim(),
                                                                    AllocateStrategy =
                                                                        this.ddlAllocateStrategy.SelectedValue,
                                                                    LineID = int.Parse(this.txtLineID.Text.Trim()),
                                                                    Descr = this.txtDesrc.Text.Trim(),
                                                                    UomID = this.ddlUomID.SelectedValue,
                                                                    PickCode = this.txtAllocateFilter.Text,
                                                                    PickCodeSql = "",
                                                                    IsActive = this.chbIsActive.Checked ? "Y" : "N",
                                                                    CreatedBy = base.UserInfo.UserID
                                                                };
                if (this.hdnType.Value == "edit")
                {
                    allocateRuleDetail.UpdatedBy = base.UserInfo.UserID;
                    if (
                        !string.IsNullOrEmpty(
                            BaseInfoDetailServiceClientProxy.GetInstance().UpdateDetail(allocateRuleDetail,
                                                                                        "ALLOCATE_RULE_DETAIL")))
                    {
                        success = true;
                    }
                }
                else if (
                    !string.IsNullOrEmpty(
                        BaseInfoDetailServiceClientProxy.GetInstance().InsertDetail(allocateRuleDetail,
                                                                                    "ALLOCATE_RULE_DETAIL")))
                {
                    success = true;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2440_005") + ex.Message;
                return;
            }
            if (success)
            {
                base.Response.Redirect(string.Format("AllocateRule.aspx?from={0}&detail={1}&ID={2}", base.UrlEncode(this.hdnFrom.Value), "1", base.UrlEncode(this.txtAllocateRuleID.Text.Trim())));
            }
        }

        protected void btnSaveContinue_Click(object sender, EventArgs e)
        {
            bool success = false;
            try
            {
                AllocateRuleDetailInfo allocateRuleDetail = new AllocateRuleDetailInfo {
                    AllocateRuleID = this.txtAllocateRuleID.Text.Trim(),
                    AllocateStrategy = this.ddlAllocateStrategy.SelectedValue,
                    LineID = int.Parse(this.txtLineID.Text.Trim()),
                    Descr = this.txtDesrc.Text.Trim(),
                    UomID = this.ddlUomID.SelectedValue,
                    PickCode = this.txtAllocateFilter.Text,
                    PickCodeSql = "",
                    IsActive = this.chbIsActive.Checked ? "Y" : "N",
                    CreatedBy = base.UserInfo.UserID
                };
                if (!string.IsNullOrEmpty(BaseInfoDetailServiceClientProxy.GetInstance().InsertDetail(allocateRuleDetail, "ALLOCATE_RULE_DETAIL")))
                {
                    success = true;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2440_005") + ex.Message;
                return;
            }
            if (success)
            {
                this.lblMsg.Text = this.txtLineID.Text + base.GetGlobalMsgResource("C004");
                if (this.ddlAllocateStrategy.Items.Count > 0)
                {
                    this.ddlAllocateStrategy.SelectedValue = this.ddlAllocateStrategy.Items[0].Value;
                }
                this.txtLineID.Text = (int.Parse(this.txtLineID.Text.Trim()) + 10).ToString();
                this.txtDesrc.Text = "";
                if (this.ddlAllocateStrategy.Items.Count > 0)
                {
                    this.ddlAllocateStrategy.SelectedValue = this.ddlAllocateStrategy.Items[0].Value;
                }
                if (this.ddlUomID.Items.Count > 0)
                {
                    this.ddlUomID.SelectedValue = this.ddlUomID.Items[0].Value;
                }
                this.txtAllocateFilter.Text = "";
            }
        }

        private void InitAllocateRuleDetailDropDownLit()
        {
            this.InitAllocateStrategies(this.ddlAllocateStrategy);
            this.InitUom(this.ddlUomID);
        }

        private void InitAllocateStrategies(DropDownList ddl)
        {
            List<SysCode4DDL> sysAllocateStrategy = CommoneMethodServiceClientProxy.GetInstance().GetSysCodeDataSource("AllocStrategies");
            ddl.DataSource = sysAllocateStrategy;
            ddl.DataTextField = "SysCodeText";
            ddl.DataValueField = "SysCodeValue";
            ddl.DataBind();
        }

        private void InitUom(DropDownList ddl)
        {
            List<SysCode4DDL> sysUom = CommoneMethodServiceClientProxy.GetInstance().GetSysCodeDataSource("TUOM");
            ddl.DataSource = sysUom;
            ddl.DataTextField = "SysCodeText";
            ddl.DataValueField = "SysCodeValue";
            ddl.DataBind();
        }

        private void LoadData()
        {
            this.InitAllocateRuleDetailDropDownLit();
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"].ToString()))
            {
                string allocateRuleID = base.Request.QueryString["ID"].ToString();
                this.txtAllocateRuleID.Text = allocateRuleID;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"].ToString()))
            {
                string from = base.Request.QueryString["from"].ToString();
                this.hdnFrom.Value = from;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["SeqNO"].ToString()))
            {
                string LineID = base.Request.QueryString["SeqNO"].ToString();
                this.txtLineID.Text = LineID;
            }
            if (base.Request.QueryString["type"] != null)
            {
                this.btnSaveContinue.Enabled = false;
                this.hdnType.Value = base.Request.QueryString["type"].ToString();
                this.SetAllocateRuleDetail2From();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetAllocateRuleDetail2From()
        {
            if ((this.txtAllocateRuleID.Text.Trim() != "") && (this.txtLineID.Text.Trim() != ""))
            {
                AllocateRuleDetailInfo allocateRuleDetail = BaseInfoDetailServiceClientProxy.GetInstance().GetDetailInfoByID(this.txtAllocateRuleID.Text.Trim(), this.txtLineID.Text.Trim(), "", "ALLOCATE_RULE_DETAIL") as AllocateRuleDetailInfo;
                if (allocateRuleDetail != null)
                {
                    this.ddlAllocateStrategy.SelectedValue = allocateRuleDetail.AllocateStrategy;
                    this.txtDesrc.Text = allocateRuleDetail.Descr;
                    this.ddlAllocateStrategy.SelectedValue = allocateRuleDetail.AllocateStrategy;
                    this.ddlUomID.SelectedValue = allocateRuleDetail.UomID;
                    this.chbIsActive.Checked = allocateRuleDetail.IsActive == "Y";
                    this.txtAllocateFilter.Text = allocateRuleDetail.PickCode;
                    this.txtAllocateRuleID.Enabled = false;
                    this.txtLineID.Enabled = false;
                }
            }
        }
    }
}

